package com.fancyy.scm.activity;


import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.Toast;

import com.fancyy.scm.R;
import com.fancyy.scm.base.BaseActivity;
import com.fancyy.scm.base.adapter.BluetoothDeviceAdapter;
import com.fancyy.scm.base.annotation.Layout;
import com.fancyy.scm.base.annotation.Translucent;
import com.fancyy.scm.bean.BluetoothDevice;
import com.fancyy.scm.databinding.ActivityConnectPrinterBinding;

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Pattern;

import zpSDK.zpSDK.zpBluetoothPrinter;

@Translucent
@Layout(R.layout.activity_connect_printer)
public class ConnectPrinterActivity extends BaseActivity<ActivityConnectPrinterBinding> {
    /**
     * 创建接口对象
     */
    private zpBluetoothPrinter mZpAPI;
    /**
     * 存储蓝牙设备列表
     */
    private List<BluetoothDevice> mBluetoothDeviceList;

    /**
     * 获取当前activity对象
     *
     * @return 返回当前activity对象
     */
    private Activity getActivity() {
        return this;
    }

    public void onBackButtonClick(View view) {
        finish();
    }
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_connect_printer);

        //初始化打印机控件
        init();
        //初始化打印机设备列表
        mBluetoothDeviceList = new ArrayList<>();
        //获取已配对打印机，遍历添加到打印机列表中
        for (int i = 0; i < mZpAPI.getAllPrinters().size(); i++) {
            mBluetoothDeviceList.add(new BluetoothDevice(mZpAPI.getAllPrinters().get(i).GetName(), mZpAPI.getAllPrinters().get(i).Getmac()));
        }
        //实例化列表控件
        ListView listView = findViewById(R.id.list_view);
        //实例化列表适配器
        BluetoothDeviceAdapter bluetoothDeviceAdapter = new BluetoothDeviceAdapter(mBluetoothDeviceList, getActivity());
        //给列表设置适配器
        listView.setAdapter(bluetoothDeviceAdapter);

        //点击列表监听
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) {
                //提示开始连接
                Toast.makeText(getActivity(), "Start Connect", Toast.LENGTH_SHORT).show();
                //连接打印机,连接之前确认是SDK所支持的打印机
                if (isSupported(mBluetoothDeviceList.get(position).getPrinterName())) {
                    //开启打印机,传入打印机MAC地址
                    if (mZpAPI.openPrinterSync(mBluetoothDeviceList.get(position).getPrinterAddress())) {
                        //提示连接成功
                        Toast.makeText(getActivity(), "Connect success", Toast.LENGTH_SHORT).show();
                        finish();
                    } else {
                        //提示连接失败
                        Toast.makeText(getActivity(), "Connect fail", Toast.LENGTH_SHORT).show();
                    }

                }
            }
        });
    }
    /**
     * 初始化打印控件
     */
    private void init() {
        //打印控件不为null,进行实例化
        if (mZpAPI == null) {
            mZpAPI = new zpBluetoothPrinter(getActivity());
        }
    }


    /**
     * 判断给定的打印机名称是否是接口所支持的打印机,防止非SDK支持打印机调用出错
     *
     * @param printerName 打印机名称
     * @return 是否支持
     */
    public boolean isSupported(String printerName) {
        return Pattern.compile("^B3" + "_\\d{4}[L]?$").matcher(printerName).matches();
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        //关闭打印机

    }

}
